t = codesters.Teacher()
tm = TestManager()
""" ONLY CHANGE WHAT IS BETWEEN THE GREEN DASHES:
---------------------------------------------------------------"""
# This array represents the board.
# 1's are blocks and 0's are open pathways.
maze_data = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 0, 0, 0, 0, 2, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 0, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 0, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 0, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 2, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
]
# This is where the turtle starts.
start_position = (2, 9)
# This is where the goal is.
goal_position = (2, 9)
# This is the direction the turtle points to begin with
start_direc = 0
# This is the scale factor for the blocks.
block_size = 50
end_result = "incomplete"
# This function iterates through the maze_data array and draws the squares.
def draw_board():
board = maze_data
for j in range(len(board)):
for i in range (len(board[j])):
if board[j][i] == 1:
block_x = (i + 0.5) * block_size - 250
block_y = 250 - (j + 0.5) * block_size
block = codesters.Square(block_x, block_y, block_size, "blue", "black")
elif board[j][i] == 2:
block_x = (i + 0.5) * block_size - 250
block_y = 250 - (j + 0.5) * block_size
# trigger_block = codesters.Square(block_x, block_y, block_size, "green", "black")
trigger_block = codesters.Sprite("key", block_x, block_y)
def create_goal():
goal_x = (goal_position[0] - 0.5) * block_size - 250
goal_y = 250 - (goal_position[1] - 0.5) * block_size
goal_block = codesters.Square(goal_x, goal_y, block_size, "lightyellow", "black")
return goal_block
# goal_block.cannot_collide()
draw_board()
goal_block = create_goal()
goal_x = (goal_position[0] - 0.5) * block_size - 250
goal_y = 250 - (goal_position[1] - 0.5) * block_size
door = codesters.Square(goal_x, goal_y, block_size, "black", "black")
txt = codesters.Text("Guide the turtle to the gold box.", 0, 225, "white")
sprite = codesters.Sprite("turtle1")
sprite.set_size(0.3)
sprite.set_speed(2)
# This function puts the turtle at the starting position and rotation.
def send_sprite_home(t):
t.set_x((start_position[0] - 0.5 ) * block_size - 250)
t.set_y(250 - (start_position[1] - .5) * block_size)
t.set_rotation(start_direc)
send_sprite_home(sprite)
trigger_count = 0
collision_state = 0
# This defines what happens when the turtle reaches the goal.
def collision(sprite, hit_sprite):
global collision_state
global goal_position
global block_size
global trigger_count
global goal_block
global door
y_pos = sprite.get_y()
color = hit_sprite.get_color()
img = hit_sprite.get_image_name()
if color == "gold":
door_op = 1.0
for counter in range(5):
door_op -= 0.2
door.set_opacity(door_op)
stage.wait(.2)
goal_x = (goal_position[0] - 0.5) * block_size - 250
goal_y = 250 - (goal_position[1] - 0.5) * block_size
sprite.glide_to(goal_x, goal_y)
sprite.reset_animation()
hit_sprite.set_color("yellow")
stage.remove_sprite(txt)
if collision_state < 1:
tm.display_success_message("Great job!")
collision_state += 1
elif color == "blue":
sprite.reset_animation()
hit_sprite.set_color("lightblue")
send_sprite_home(sprite)
stage.remove_sprite(txt)
if collision_state < 1:
tm.display_failure_message("You hit a block. Try again.")
collision_state += 1
elif img == "key":
stage.remove_sprite(hit_sprite)
trigger_count += 1
if trigger_count == 3:
goal_block.set_color("gold")
sprite.event_collision(collision)
sprite.pen_down()